package example.controller;

import com.alibaba.excel.EasyExcel;
import example.common.Result;
import example.common.Temp;
import example.dto.AddSecondmentDTO;
import example.dto.GetSecListDTO;
import example.dto.SearchSecondmentDTO;
import example.dto.SecondmentConfirmDTO;
import example.entity.Secondment;
import example.entity.SecondmentContentInfo;
import example.entity.SecondmentDistributeInfo;
import example.entity.TaskList;
import example.listener.BatchReadListener;
import example.mapper.DepartmentMapper;
import example.service.*;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/secondment")
public class SecondmentController {
    @Resource
    private ISecondmentService secondmentService;

    @Resource
    private ISecondmentHeadInfoService secondmentHeadInfoService;

    @Resource
    private ISecondmentContentInfoService secondmentContentInfoService;

    @Resource
    private ISecondmentDistributeInfoService secondmentDistributeInfoService;

    @Resource
    private IUserInfoService userInfoService;

    @Resource
    private ITaskListService taskListService;

    @Resource
    private DepartmentMapper departmentMapper;

    /** 上传excel模板文件 */
    @PostMapping("/upload/excel")
    @Transactional(propagation = Propagation.REQUIRED)
    public Result uploadExcel(MultipartFile file, Long secondmentId) {
        Temp.secondmentId = secondmentId;
        try {
            EasyExcel.read(file.getInputStream(), new BatchReadListener(secondmentHeadInfoService, secondmentContentInfoService, userInfoService,departmentMapper)).sheet().doRead();
        } catch (Exception e) {
            return Result.fail("上传文件失败！请按模板上传excel并检查人员信息！");
        }
        return Result.ok("上传文件成功！");
    }

    /** 添加借调事项 */
    @PostMapping("/add")
    public Result addSecondment(@RequestBody AddSecondmentDTO addSecondmentDTO) {
        return secondmentService.addSecondment(addSecondmentDTO);
    }

    @PostMapping("/list")
    public Result listSecondmentByDepartmentId(@RequestBody GetSecListDTO getSecListDTO) {
        return secondmentService.listSecondmentByDepartmentId(getSecListDTO);
    }

    @PostMapping("/getC")
    public Result getC(@RequestBody SecondmentContentInfo secondmentContentInfo) {
        return secondmentService.getC(secondmentContentInfo);
    }

    @PostMapping("/lis")
    public Result lisSecondmentByDepartmentId(@RequestBody GetSecListDTO getSecListDTO) {
        return secondmentService.lisSecondmentByDepartmentId(getSecListDTO);
    }

    /** 删除指定id的借调事项 */
    @GetMapping("/delete/{id}")
    public Result deleteSecondment(@PathVariable("id") Long secondmentId) {
        return secondmentService.deleteSecondment(secondmentId);
    }

    /** 上传pdf文件 */
    @PostMapping("/upload/pdf")
    public Result uploadPdfFile(@RequestParam List<MultipartFile> files, @RequestParam Long secondmentId) {
        return secondmentDistributeInfoService.uploadPdfFile(files, secondmentId);
    }

    @PostMapping("/con/pdf")
    public Result uploadConfirmFile(@RequestParam MultipartFile file, @RequestParam Long secondmentId, @RequestParam Long id) {
        return secondmentDistributeInfoService.uploadConfirmFile(file, secondmentId);
    }

    @PostMapping("/single/pdf")
    public Result singleConfirmSecondment(@RequestParam MultipartFile file, @RequestParam Long secondmentId, @RequestParam Long id) {
        return secondmentService.singleConfirmSecondment(file, secondmentId, id);
    }
    @PostMapping("/alldepart/pdf")
    public Result alldepartConfirmSecondment(@RequestParam MultipartFile file, @RequestParam Long secondmentId) {
        return secondmentService.alldepartConfirmSecondment(file, secondmentId);
    }
    @PostMapping("/onedepart/pdf")
    public Result onedepartConfirmSecondment(@RequestParam MultipartFile file, @RequestParam Long secondmentId ,@RequestParam Long departmentId ) {
//        return secondmentService.alldepartConfirmSecondment(file, secondmentId);
        return secondmentService.onedepartConfirmSecondment(file, secondmentId,departmentId);
    }
//    /** 分发借调事项 */
//    @PostMapping("/distribute")
//    public Result distributeCase(@RequestParam Long secondmentId, @RequestParam List<Long> departmentIds) {
//        return secondmentDistributeInfoService.distributeCase(secondmentId, departmentIds);
//    }

    @PostMapping("/distribute")
    public Result distributeCase(@RequestParam Long secondmentId, @RequestParam List<String> departmentNames) {
        return secondmentDistributeInfoService.distributeCase(secondmentId, departmentNames);
    }
    @PostMapping("/check")
    public Result checkNumOfSecondment(@RequestParam Long secondmentId, @RequestParam List<String> departmentNames) {
        return secondmentDistributeInfoService.checkNumOfSecondment(secondmentId, departmentNames);
    }

    @GetMapping("/file/{id}")
    public ResponseEntity<byte[]> getResponseZipFile(@PathVariable("id") Long secondmentId) {
        return secondmentService.getResponseZipFile(secondmentId);
        //return Down.downloadFolder();
    }

    @GetMapping("/temp/{id}")
    public ResponseEntity<byte[]> getTempZipFile(@PathVariable("id") Long secondmentId) {
        return secondmentService.getTempZipFile(secondmentId);
        //return Down.downloadFolder();
    }

    @GetMapping("/zip/name/{id}")
    public Result getZipName(@PathVariable("id") Long secondmentId) {
        return secondmentService.getZipName(secondmentId);
    }

    @GetMapping("/pdf/name/{id}")
    public Result getPdfName(@PathVariable("id") Long secondmentId) {
        return secondmentService.getPdfName(secondmentId);
    }


    @GetMapping("/confirm/name/{id}")
    public Result getConfirmName(@PathVariable("id") Long secondmentId) {
        return secondmentService.getConfirmName(secondmentId);
    }
    @PostMapping("/secondmentName")
    public Result updateSecondmentName(@RequestBody Secondment secondment) {
        return secondmentService.updateSecondmentName(secondment);
    }

    @PostMapping("/upload/to")
    public Result uploadToLevelOne(@RequestParam MultipartFile file, @RequestParam Long secondmentId) {
        return secondmentService.uploadToLevelOne(file, secondmentId);
    }

    @GetMapping("/state/{id}")
    public Result changeSecondmentState(@PathVariable("id") Long secondmentId) {
        return secondmentService.changeSecondmentState(secondmentId);
    }

    @PostMapping("/search")
    public Result searchUser(@RequestBody SearchSecondmentDTO searchSecondmentDTO) {
        return secondmentService.searchUser(searchSecondmentDTO);
    }

    @GetMapping("/confirm/{id}")
    public Result confirmSecondment(@PathVariable("id") Long id) {
        return secondmentService.confirmSecondment(id);
    }

    @PostMapping("/confirm/state")
    public Result changeConfirmState(@RequestBody SecondmentConfirmDTO secondmentConfirmDTO) {
           return secondmentService.changeConfirmState(secondmentConfirmDTO);
    }
   //确认借调起止时间
    @PostMapping("/confirm/time")
    public Result confirm(@RequestBody TaskList taskList) {
        return taskListService.confirmTime(taskList);
    }
}

